import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/res/colors.dart';
import 'package:ls_work/util/ext_utils.dart';
import 'package:ls_work/viewmodel/medecine/medicine_lack_list_view_model.dart';

import '../../res/gaps.dart';
import '../../widgets/common_widget.dart';
class MedicineLackListPage extends BaseStatefulMvvmWidget{
  @override
  State<StatefulWidget> createState() => _MedicineLackListState();

}

class _MedicineLackListState extends BaseMvvmListState<MedicineLackListViewModel,MedicineLackListPage>{

  GlobalKey<ScaffoldState> key = GlobalKey();

  @override
  Widget buildRootView(BuildContext context, Widget loadingContentWidget) {
    return Scaffold(
      backgroundColor: Colours.bg_color,
      key: key,
      appBar: AppBar(
        title: Text('缺药清单'),
        actions: [
          IconButton(onPressed: (){
            key.currentState?.openEndDrawer();
          }, icon: Icon(Icons.search))
        ],
      ),
      body: loadingContentWidget,
      endDrawer: endDraw(),
    );
  }

  Widget endDraw() {
    return Column(
      children: [
        AppBar(
          leading: Container(),
          actions: [Container()],
          title: Text('筛选'),
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            SizedBox(
              height: 12,
            ),
            CommonText('长者姓名'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入长者姓名',
                text: vm?.searchName ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  viewModel.searchName = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(
              height: 10,
            ),
            CommonText('房间号'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入房间号',
                text: vm?.roomName ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  viewModel.roomName = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(height: 10,),

            CommonText('药品名称'),
            SizedBox(
              height: 8,
            ),
            NoBorderInput(
                hintText: '输入药品名称',
                text: vm?.medicineName ?? "",
                maxLines: 1,
                textInputAction: TextInputAction.search,
                onChanged: (str) {
                  viewModel.medicineName = str;
                },
                onSubmitted: (str) {
                  viewModel.requestRefresh();
                })
                .addToContainer(
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)),

            SizedBox(height: 10,),

            CommonText('缺药登记日期'),
            SizedBox(
              height: 8,
            ),
            CommonText(vm?.lackDate,hintText: '请选择')
                .addToContainer(
                width: double.infinity,
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)).onTap(() {
              viewModel.chooseLackDate();
            }),

            SizedBox(height: 10,),

            CommonText('是否缺药'),
            SizedBox(
              height: 8,
            ),
            CommonText(vm?.lackStatus?.pickName??"",hintText: '请选择')
                .addToContainer(
                width: double.infinity,
                decoration: BoxDecoration(
                    color: Colours.bg_color, borderRadius: 6.borderRadius),
                padding: EdgeInsets.symmetric(horizontal: 10, vertical: 5)).onTap(() {
              viewModel.chooseLackStatus();
            }),
            SizedBox(height: 10,),
          ],
        ).addToContainer(
          padding: EdgeInsets.symmetric(horizontal: 16),
        ),
        SizedBox().setWeight(1),
        Gaps.dividerLine,
        SizedBox(height: 10,),
        Row(
          children: [
            SizedBox(width: 16,),
            TextButton(
              onPressed: () {
                key.currentState?.closeEndDrawer();
                viewModel.resetClick();
              },
              child: CommonText('重置',textColor: Colors.blue),
              style: ButtonStyle(
                  shape: WidgetStateProperty.all(RoundedRectangleBorder(
                      borderRadius: BorderRadius.horizontal(left: 20.radius),
                      side: BorderSide(color: Colors.blue)
                  ))
              ),)
                .setWeight(1),
            TextButton(
                onPressed: () {
                  key.currentState?.closeEndDrawer();
                  viewModel.requestRefresh();
                },
                child: CommonText('确定',textColor: Colors.white),
                style: ButtonStyle(
                    backgroundColor: WidgetStateProperty.all(Colors.blue),
                    shape: WidgetStateProperty.all(RoundedRectangleBorder(
                      borderRadius: BorderRadius.horizontal(right: 20.radius),
                      side: BorderSide(color: Colors.blue),
                    ))
                ))
                .setWeight(1),
            SizedBox(width: 16,),
          ],
        ),
        SizedBox(height: 12,)
      ],
    ).addToContainer(
        width: MediaQuery.of(context).size.width * 0.7,
        decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.horizontal(left: 12.radius)));
  }

  @override
  Widget createItemWidget(int index) {
    var item = viewModel.listItems[index];
    return Stack(
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            CommonText('${item.drugName}',textColor:item.status==1? Colors.red: Colors.blue,fontWeight: FontWeight.bold,textSize: 18),
            CommonText('药品规格：${item.specifications}'),
            CommonText('缺药登记日期：${(item.lackDate??"").changeTime(format: [yyyy,'-',mm,'-',dd])}'),
            CommonText('药品实时库存：${item.quantity?.d}${item.minUnit}'),
            CommonText('最小单位（配药单位）: ${item.minUnit}'),
            Row(
              children: [
                CommonText('是否缺药：'),
                CommonText('${item.statusName}',textColor: item.status==1? Colors.red:Colors.black)
              ],
            )
          ].insertWidget(SizedBox(height: 8,)),
        ),
        
        Column(
          children: [
            CommonText('${item.customerName}',fontWeight: FontWeight.bold,textSize: 17),
            SizedBox(height: 10,),
            CommonText('${item.bedNo??""}')
          ],
        ).setLocation(top: 40,right: 10)
      ],
    ).addToContainer(
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: 6.borderRadius
      ),
      padding: EdgeInsets.symmetric(horizontal: 10,vertical: 12)
    );
  }

  @override
  MedicineLackListViewModel createViewModel() {
    return MedicineLackListViewModel(context);
  }

  @override
  Widget get separatorDivider => SizedBox(height: 10,);

  @override
  EdgeInsets get listPadding => EdgeInsets.symmetric(horizontal: 16,vertical: 12);

  @override
  bool get canPullUp => true;
}